package com.itcast.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itcast.domain.Good;
import com.itcast.service.GoodService;
import com.itcast.utils.ResponseBodyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;


@Controller
@CrossOrigin
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 查询所有  无需参数
     * @return
     */
    @RequestMapping(value = "/findAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Good> findAll(){
        List<Good> list = goodService.findAll();
        return list;
    }

    /**
     * 通过id查询  在路径上给id
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Good findById(@PathVariable("id") String id){
        Good good = goodService.findById(id);
        return good;
    }

    /**
     * 通过name模糊查询   给一个name的键值对
     * @param json
     * @return
     */
    @RequestMapping(value = "/findByNameLike", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Good> findByNameLike(@RequestBody String json) throws Exception {
        Good good = mapper.readValue(json, Good.class);
        List<Good> list = goodService.findByNameLike(good.getName());
        return list;
    }

    /**
     * 创建商品  id不给，其余随意
     * @param json
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(@RequestBody String json) throws Exception {
        Good good = mapper.readValue(json, Good.class);
        int count = goodService.saveGood(good);
        good = goodService.findByName(good.getName());
        if (count > 0){
            return ResponseBodyUtil.getSuccess(good,"创建");
        } else {
            return ResponseBodyUtil.getUnSuccess(good,"创建");
        }
    }

    /**
     * 修改商品
     * @param json  id是必须的，其余随意
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(@RequestBody String json) throws Exception {
        Good good = mapper.readValue(json, Good.class);
        int count = goodService.update(good);
        good = goodService.findById(String.valueOf(good.getId()));
        if (count > 0){
            return ResponseBodyUtil.getSuccess(good,"修改");
        } else {
            return ResponseBodyUtil.getUnSuccess(good,"修改");
        }
    }

    /**
     * 删除单个商品  路径上给定id
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(@PathVariable("id") String id) throws Exception {
        Good good = goodService.findById(id);
        int count = goodService.delete(id);
        if (count > 0){
            return ResponseBodyUtil.getSuccess(good,"删除");
        } else {
            return ResponseBodyUtil.getUnSuccess(good,"删除");
        }
    }

    /**
     * 批量删除  路径上给id 以1,3,5形式给
     * @param str
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/batchDelete/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String batchDelete(@PathVariable("id") String str) throws Exception {
        List<String> list = Arrays.asList(str.split(","));
        return goodService.batchDelete(list);
    }

    /**
     * 批量上线  路径上给id 以1,3,5形式给
     * @param str
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/isOnline/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String isOnline(@PathVariable("id") String str) throws Exception {
        List<String> list = Arrays.asList(str.split(","));
        return goodService.isOnline(list);
    }

    /**
     * 批量下线  路径上给id 以1,3,5形式给
     * @param str
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/offLine/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String offLine(@PathVariable("id") String str) throws Exception {
        List<String> list = Arrays.asList(str.split(","));
        return goodService.offLine(list);
    }
}
